package com.example.caimogu.businessapp;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.caimogu.Goods2Json.JsonUtil;
import com.example.caimogu.bean.Account;
import com.example.caimogu.bean.Business;
import com.example.caimogu.http.HttpClientUtil;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class SignUpActivity extends AppCompatActivity {

    private EditText etAccName;
    private EditText etAccPsd;
    private EditText etName;
    private EditText etPhone;
    private EditText etInfo;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_up);
        initEt();
    }

    private void initEt() {
        etAccName = (EditText) findViewById(R.id.et_signup_accname);
        etAccPsd = (EditText) findViewById(R.id.et_signup_accpsd);
        etName = (EditText) findViewById(R.id.et_signup_name);
        etPhone = (EditText) findViewById(R.id.et_signup_phone);
        etInfo = (EditText) findViewById(R.id.et_signup_info);
    }

    public void commitNewBusiness(View view){
       if(etAccName.getText().length()==0 &&
               etAccPsd.getText().length()==0 &&
               etName.getText().length()==0 &&
               etPhone.getText().length()==0 &&
               etInfo.getText().length()==0 ){
           Toast.makeText(this,"信息没写全",Toast.LENGTH_SHORT).show();
           return;

       }

        new Thread(new Runnable() {
            @Override
            public void run() {
                signUpSucc();
            }
        }).start();
       Intent intent = new Intent();
       intent.putExtra("signUpSuccess", "true");
       setResult(2, intent);
       this.finish();

   }

    //提交注册信息，等待返回确认
    public boolean signUpSucc(){
        Log.i("caimogu","signUpSucc........in");
        List<Business> buslist = new ArrayList<Business>();
        Business bus = new Business();
        Account acc = new Account();
        acc.setAccount(etAccName.getText().toString());
        acc.setPsd(etAccPsd.getText().toString());
        bus.setAcc(acc);
        bus.setName(etName.getText().toString());
        bus.setPhone(etPhone.getText().toString());
        bus.setInformation(etInfo.getText().toString());
        bus.setStar(5);
        buslist.add(bus);

        JsonUtil jsonUtil = new JsonUtil();
        try {
            JSONArray jsonArray = jsonUtil.listBusiness2Json(buslist);

            HttpClientUtil httpClientUtil = new HttpClientUtil();
            httpClientUtil.sendBus2Server(jsonArray);

        }catch (Exception e){
            e.printStackTrace();
        }
        Log.i("caimogu", "signUpSucc........out");
        return true;
    }

//    private void analyseResponse(HttpResponse response) throws IOException {
//        HttpEntity entity = response.getEntity();
//
//        String strResult = EntityUtils.toString(entity);
//
//        Log.i("caimogu",strResult);
//    }


}
